package com.xnxy.design.service;

import com.github.pagehelper.PageHelper;
import com.xnxy.design.mapper.CoreMapper;
import com.xnxy.design.util.PageData;

import java.util.List;

/**
 * 业务逻辑层工具类
 *
 * @param <T>
 * @param <M>
 * @author puzengcheng
 * @date 2018/10/13
 */
public abstract class CoreService<T, M> {

    private Integer flagFalse = 0;

    /**
     * 接收子类注入的mapper类
     *
     * @return
     */
    public abstract CoreMapper<T, M> getMapper();


    /**
     * 通过id删除数据实现
     *
     * @param id
     * @return
     */
    public T selectByPrimaryKey(M id) {
        return getMapper().selectByPrimaryKey(id);
    }

    /**
     * 分页查询方法实现
     *
     * @return
     */
    public PageData queryData(Integer pageNum, Integer pageSize, List<String> list) {
        PageHelper.startPage(pageNum, pageSize);
        List<T> bizUsrList = getMapper().getAll(list);
        PageData pageData = new PageData();
        pageData.setListData(bizUsrList);
        pageData.setPageNum(pageNum);
        pageData.setPageSize(pageSize);
        Integer size = getMapper().size();
        pageData.setCountPage(size);
        pageData.setPageTotal((size / pageSize) % 10 == 0 ? size / pageSize : size / pageSize + 1);
        return pageData;
    }

    /**
     * 增加数据实现
     *
     * @param obj
     * @return
     */
    public Boolean insert(T obj) {
        int flag = getMapper().insert(obj);
        if (flag != flagFalse) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改数据实现
     *
     * @param obj
     * @return
     */
    public Boolean updata(T obj) {
        int flag = getMapper().updateByPrimaryKey(obj);
        if (flag != flagFalse) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过id删除方法实现
     *
     * @param id
     * @return
     */
    public Boolean delect(M[] id) {
        int flag = getMapper().deleteByPrimaryKey(id);
        if (flag != flagFalse) {
            return true;
        } else {
            return false;
        }
    }

    public List selectAll(){
        return getMapper().selectAll();
    }
}
